package com.electronic.utils;


import com.electronic.domain.RequestUser;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
 * @Author: IFCA
 * @Description: 请求用户工具类
 */
@Slf4j
public class IRequestUtil {

    /**
     * 当前请求信息
     */
    private static final ThreadLocal<RequestUser> requestThreadLocal = new ThreadLocal<>();

    /**
     * 设置请求用户信息
     * @param requestUser
     */
    public static void setRequestUser(RequestUser requestUser) {
        requestThreadLocal.set(requestUser);
    }

    /**
     * 获取请求用户信息
     * @return
     */
    public static RequestUser getRequestUser() {
        return requestThreadLocal.get();
    }

    /**
     * 获取请求应用id
     * @return
     */
    public static String getApplicationId() {
        RequestUser requestUser = getRequestUser();
        return Objects.nonNull(requestUser) ? requestUser.getApplicationId() : null;
    }

    /**
     * 获取请求应用用户id
     * @return
     */
    public static String getApplicationUserId() {
        RequestUser requestUser = getRequestUser();
        return Objects.nonNull(requestUser) ? requestUser.getApplicationUserId() : null;
    }

    /**
     * 获取请求业务唯一流水号
     * @return
     */
    public static String getReqTransactionId() {
        RequestUser requestUser = getRequestUser();
        return Objects.nonNull(requestUser) ? requestUser.getReqTransactionId() : null;
    }

    /**
     * 获取业务系统请求流水号
     * @return
     */
    public static String getBizNo(){
        RequestUser requestUser = getRequestUser();
        return Objects.nonNull(requestUser) ? requestUser.getBizNo() : null;
    }

    /**
     * 删除缓存信息
     */
    public static void remove() {
        requestThreadLocal.remove();
    }
}
